let server = {
  server: {
    proxy: {
      "/api": {
        target: "http://127.0.0.1:5500",
        changeOrigin: true,
        secure: false,
        ws: false,
        pathRewrite: {
          "^/api": "",
        },
      },
      "/another-api": {
        target: "http://127.0.0.1:3000",
        changeOrigin: true,
        secure: false,
        ws: false,
        pathRewrite: {
          "^/another-api": "/new-path",
        },
      },
      "/yet-another-api": {
        target: "http://127.0.0.1:4000",
        changeOrigin: true,
        secure: false,
        ws: false,
        pathRewrite: {
          "^/yet-another-api": "/another-new-path",
        },
      },
    },
  },
};

console.log(JSON.stringify(server, null, 2));
let proxy = server.server.proxy;

// 遍历 proxy 对象中的所有路径
for (let path in proxy) {
  if (proxy.hasOwnProperty(path)) {
    // 获取 pathRewrite 对象
    let pathRewrite = proxy[path].pathRewrite;
    // 检查 pathRewrite 是否存在
    if (pathRewrite) {
      // 创建一个 rewrite 函数，该函数能够处理所有 pathRewrite 规则
      for (let key in pathRewrite) {
        proxy[path].rewrite = (url) => {
          if (pathRewrite.hasOwnProperty(key)) {
            return url.replace(new RegExp(key, "g"), pathRewrite[key]);
          }
        };
      }
    }
  }
}

console.log(proxy["/api"].rewrite("/api/static/data.json"));
